package org.iteam.xlz.core.service.images;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.UUID;

import org.apache.commons.lang3.StringUtils;
import org.iteam.commons.utils.DateUtil;
import org.iteam.core.utils.SqlBuilder;
import org.iteam.db.BaseService;
import org.iteam.xlz.core.entity.images.Images;
import org.iteam.xlz.core.entity.images.ImagesDelete;
import org.iteam.xlz.core.entity.images.ImagesTemp;

public class ImagesService extends BaseService<Images>{

	static ImagesService instance;
	private List<Object> params=new ArrayList<Object>();
	private StringBuilder sql=null;
	
	private ImagesService(){}
	
	public static ImagesService getInstance(){
		if(null==instance){
			instance=new ImagesService();
		}
		return instance;
	}
	
	public List<Images> findListByObjectId(String objectid){
		params.clear();
		sql=new StringBuilder("select * from images where 1=1");
		if(StringUtils.isNotBlank(objectid)){
			sql.append(" and objectid=?");
			params.add(objectid);
		}
		try {
			return super.query(sql.toString(),params.toArray());
		} catch (SQLException e) {
			e.printStackTrace();
			return new ArrayList<Images>();
		}
	}
	
	public boolean DrupYesterDayTemp(){
		Connection con=null;
		try {
			con=super.getConnection(false);
			Calendar c=Calendar.getInstance();
			c.add(Calendar.DAY_OF_MONTH,-7);
			int month=c.get(Calendar.MONTH)+1;
			String time=c.get(Calendar.YEAR)+"-"+((month+"").length()==1?("0"+month):month)+"-"+c.get(Calendar.DAY_OF_MONTH)+" 00:00:00";
			List<ImagesTemp> list= super.query(con,ImagesTemp.class,"select * from imagestemp where 1=1 and createtime<?",time);
			int i=0,j=0;
			for(ImagesTemp it:list){
				i+=super.updates(con,"delete from imagestemp where 1=1 and imgid=?",it.getImgid());
				j+=super.updates(con,"insert into imagesdelete(path) values(?)",it.getImgaddr());
			}
			if(i==list.size()&&j==list.size()){
				super.commit(con);
				return true;
			}else{
				super.rollback(con);
				return false;
			}
		} catch (SQLException e) {
			e.printStackTrace();
			try {
				super.rollback(con);
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
		}
		return false;
	}
	
	public List<ImagesDelete> getAll(){
		try {
			return super.query(ImagesDelete.class,"select * from imagesdelete where 1=1");
		} catch (SQLException e) {
			e.printStackTrace();
			return new ArrayList<ImagesDelete>();
		}
	}
	
	public boolean clear(){
		try {
			return super.updates("delete from imagesdelete")>0;
		} catch (SQLException e) {
			e.printStackTrace();
			return false;
		}
	}
	
	public boolean putInTemp(String path){
		try {
			return super.updates("insert into imagesdelete(path) values(?)",path)>0;
		} catch (SQLException e) {
			e.printStackTrace();
			return false;
		}
	}
	
	public boolean addToDelete(String path){
		try {
			return super.updates("insert into imagesdelete(path) values(?)",path)>0;
		} catch (SQLException e) {
			e.printStackTrace();
			return false;
		}
	}
	
	public String createImages(Images model){
		Connection con=null;
		try {
			con=super.getConnection();
			con.setAutoCommit(false);
			
			String id=UUID.randomUUID().toString().replace("-","").toUpperCase();
			model.setImgid(id);
			model.setCreatetime(DateUtil.currentTime());
			SqlBuilder builder=SqlBuilder.createInsert("images");
			builder.addField("imgid",model.getImgid());
			builder.addField("imgaddr",model.getImgaddr());
			builder.addField("objectid",model.getObjectid());
			if(StringUtils.isNotBlank(model.getImgalt())){
				builder.addField("imgalt",model.getImgalt());
			}
			if(StringUtils.isNotBlank(model.getImglink())){
				builder.addField("imglink",model.getImglink());
			}
			if(StringUtils.isNotBlank(model.getImgname())){
				builder.addField("imgname",model.getImgname());
			}
			builder.addField("ismain",model.getIsmain());
			builder.addField("status",model.getStatus());
			builder.addField("createtime",model.getCreatetime());
			builder.addField("creator",model.getCreator());
			builder.addField("imgtype",model.getImgtype());
			
			int i=super.updates(con,builder.getSql(),builder.getParams());
			int j=super.updates(con,"delete from imagesdelete where 1=1 and path=?",model.getImgaddr());
			if(i==1&&j==1){
				super.commit(con);
				return id;
			}else{
				super.rollback(con);
				return null;
			}
		} catch (SQLException e) {
			e.printStackTrace();
			try {
				super.rollback(con);
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
		}
		return null;
	}
	
	public boolean updateImages(Images model){
		Connection con=null;
		try{
			con=super.getConnection();
			con.setAutoCommit(false);

			String oldAddr=super.show("select * from images where 1=1 and imgid=?",model.getImgid()).getImgaddr();
			
			SqlBuilder builder=SqlBuilder.createUpdate("images");
			builder.addField("imgaddr",model.getImgaddr());
			builder.addField("imgalt",model.getImgalt());
			builder.addField("imglink",model.getImglink());
			builder.addField("imgname",model.getImgname());
			builder.addField("ismain",model.getIsmain());
			builder.addField("status",model.getStatus());
			builder.addField("imgtype",model.getImgtype());
			builder.addWhere("imgid",model.getImgid());
			
			int i=super.updates(con,builder.getSql(),builder.getParams());
			int j=0;
			int k=0;
			if(oldAddr.equals(model.getImgaddr())){
				j=1;
				k=1;
			}else{
				j=super.updates(con,"insert into imagesdelete(path) values(?)",oldAddr);
				k=super.updates(con,"delete from imagesdelete where path=?",model.getImgaddr());
			}
			if(i==1&&j==1&&k==1){
				super.commit(con);
				return true;
			}else{
				super.rollback(con);
				return false;
			}			
		} catch (SQLException e) {
			e.printStackTrace();
			try {
				super.rollback(con);
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
		}
		return false;
	}
	
	public boolean deleteImage(Images model){
		Connection con=null;
		try{
			con=super.getConnection();
			con.setAutoCommit(false);
			
			int i=super.updates(con,"delete from images where imgid=?",model.getImgid());
			int j=super.updates(con,"insert into imagesdelete(path) values(?)",model.getImgaddr());
			if(i==1&&j==1){
				super.commit(con);
				return true;
			}else{
				super.rollback(con);
				return false;
			}			
		} catch (SQLException e) {
			e.printStackTrace();
			try {
				super.rollback(con);
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
		}
		return false;
	}
}
